package com.xh.bussiness.permission.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.permission.bean.request.RoleMenuRequest;
import com.xh.bussiness.permission.dao.RoleMenuMapper;
import com.xh.bussiness.permission.model.Menu;
import com.xh.bussiness.permission.model.Role;
import com.xh.bussiness.permission.model.RoleMenu;
import com.xh.bussiness.permission.service.IMenuService;
import com.xh.bussiness.permission.service.IRoleMenuService;
import com.xh.bussiness.permission.service.IRoleService;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


@Service
public class RoleMenuServiceImpl extends BaseServiceImpl<RoleMenuMapper, RoleMenu> implements IRoleMenuService {

    @Autowired
    private IRoleService roleService;
    @Autowired
    private IMenuService menuService;

    @Override
    public List queryRoleMenu(RoleMenuRequest bean) {
        QueryWrapper qw=new QueryWrapper<RoleMenu>();
        //拼装条件
        qw=prepareParams(bean, qw);
        List<RoleMenu> list = this.dao.selectList(qw);
        if (!CollectionUtils.isEmpty(list)) {
            List<RoleMenu> newResult = new ArrayList<>();
            //前端需求：去除带有子节点的根节点
            for (int i = 0; i < list.size(); i++) {
                List<Menu> child = menuService.getChild(list.get(i).getMenuId());
                if (CollectionUtils.isEmpty(child)) {
                    newResult.add(list.get(i));
                }
            }
            return newResult;
        }

        return null;
    }


    @Override
    public String addRoleMenu(RoleMenuRequest bean) {
        //先删除角色与菜单关系
        deleteBatch(new String[]{bean.getRoleId()});

        for (String menuId : bean.getMenuIds()) {
            RoleMenu roleMenu=new RoleMenu();
            roleMenu.setRoleId(bean.getRoleId());
            roleMenu.setMenuId(menuId);
            this.insert(roleMenu);
        }
        return null;
    }


    public int deleteBatch(String[] roleIds){
        return this.dao.deleteBatch(roleIds);
    }

    @Override
    public String modifyRoleMenu(RoleMenuRequest bean) {
        //先删除角色与菜单关系
        deleteBatch(new String[]{bean.getRoleId()});

        Role role = roleService.getById(bean.getRoleId());
        if(role==null){
            return "当前角色菜单id不存在";
        }
        for (String menuId : bean.getMenuIds()) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(bean.getRoleId());
            roleMenu.setMenuId(menuId);
            this.insert(roleMenu);
        }
        return null;
    }


    @Override
    public String deleteRoleMenu(String id) {
        RoleMenu roleMenu=this.getById(id);
        if(roleMenu==null){
            return "当前角色菜单id不存在";
        }

        this.dao.deleteById(id);

        return null;
    }


    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(RoleMenuRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        if (!StringUtils.isEmpty(bean.getRoleId())) {
            qw.eq("role_id", bean.getRoleId());
        }
        return qw;
    }
}
